package com.wsj.util.tool;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO; 

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtils { 
	 

	/**
	 * ͼƬ��ߵ���
	 * @param file �ļ�����
	 * @param maxw �����
	 * @param maxh ���߶�
	 */
public static void imgresize(String imgin, int maxwidth,int maxheight){
	boolean goon=false;
	if(maxwidth==0||maxheight==0) goon=false;
        BufferedImage bis=null;
        BufferedImage bid=null;
        File file =new File(imgin);
        try{  
//*****************************************************************
//ͼƬ��ߵ���
            bis = ImageIO.read(file);
            int c_w= bis.getWidth();
            int c_h= bis.getHeight();

            if(maxwidth==0) {
            	if(c_h>maxheight) {
            		maxwidth=c_w*maxheight/c_h;
            		goon=true;
            	}else {
            		bis=null;
            	}
            }else if(maxheight==0) {
            	if(c_w>maxwidth) {
            		maxheight=c_h*maxwidth/c_w;
            		goon=true;
            	}else {
            		bis=null;
            	}
            }else {
            	if(c_h/c_w>maxheight/maxwidth) {
            		if(c_h>maxheight) {
                		maxwidth=c_w*maxheight/c_h;
                		goon=true;
                	}else {
                		bis=null;
                	}
            	}else {
            		if(c_w>maxwidth) {
                		maxheight=c_h*maxwidth/c_w;
                		goon=true;
                	}else {
                		bis=null;
                	}
            	}
            }
//************************************************************************
       if(goon) {     
            bid= new BufferedImage(maxwidth, maxheight,BufferedImage.TYPE_INT_RGB);                        
            Graphics g = bid.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0,0, maxwidth, maxheight);           
            g.drawImage(bis,0,0,maxwidth, maxheight,null);  
             ImageIO.write(bid, "jpeg", file);
       		}else {
       			ImageIO.write(ImageIO.read(file), "jpeg", file);
       		} 
        } catch (Exception e){
                      
        }
        bis=null;        
        bid=null;
    }
	


	/** 
	* ��ͼƬӡˢ��ͼƬ�� 
	* @param pressImg -- ˮӡ�ļ� 
	* @param targetImg -- Ŀ���ļ� 
	* @param x 
	* @param y 
	*/ 
	public  static void pressImage(String pressImg, String targetImg, int x, int y) { 
	try { 
	File _file = new File(targetImg); 
	Image src = ImageIO.read(_file); 
	int wideth = src.getWidth(null); 
	int height = src.getHeight(null); 
	BufferedImage image = new BufferedImage(wideth, height, 
	BufferedImage.TYPE_INT_RGB); 
	Graphics g = image.createGraphics(); 
	g.drawImage(src, 0, 0, wideth, height, null); 

//	 ˮӡ�ļ� 
	File _filebiao = new File(pressImg); 
	Image src_biao = ImageIO.read(_filebiao); 
	int wideth_biao = src_biao.getWidth(null); 
	int height_biao = src_biao.getHeight(null); 
	g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao, 
	height_biao, null); 
//	 / 
	g.dispose(); 
	FileOutputStream out = new FileOutputStream(targetImg); 
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
	encoder.encode(image); 
	out.close(); 
	} catch (Exception e) { 
	e.printStackTrace(); 
	} 
	} 

	
	
	/** 
	* ��ӡ����ˮӡͼƬ 
	* @param pressText --���� 
	* @param targetImg -- Ŀ��ͼƬ 
	* @param fontName -- ������ 
	* @param fontStyle -- ������ʽ 
	* @param color -- ������ɫ 
	* @param fontSize -- �����С 
	* @param x -- ƫ�� 
	* @param y 
	*/ 

	public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) { 
	try { 
	File _file = new File(targetImg); 
	Image src = ImageIO.read(_file); 
	int wideth = src.getWidth(null); 
	int height = src.getHeight(null); 
	BufferedImage image = new BufferedImage(wideth, height, 
	BufferedImage.TYPE_INT_RGB); 
	Graphics g = image.createGraphics(); 
	g.drawImage(src, 0, 0, wideth, height, null); 
	g.setColor(Color.darkGray ); 
	g.setFont(new Font(fontName, fontStyle, fontSize)); 
	int size=pressText.getBytes().length;
	g.drawString(pressText, wideth - size*fontSize/2 - x, height - fontSize/2 - y); 
	g.dispose(); 
	FileOutputStream out = new FileOutputStream(targetImg); 
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
	encoder.encode(image); 
	out.close(); 
	} catch (Exception e) {
		e.printStackTrace();
	} 
	} 


    public static void main(String[] args){
    		
    	 //mgresize("e:\\a.jpg",3000,0);
    	 ///pressImage("e:\\b.png", "e:\\a.jpg", 0 ,0);
    	// pressText("www.13nr.com","E:\\a.jpg","����",Font.PLAIN,0,14,10,-14*3/8);
    }
}